package com.example.opengldemo.jbulletJava;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.opengl.GLES30;

import com.bulletphysics.collision.shapes.*;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.Transform;

import javax.vecmath.Vector3f;

public class TexFloor {

    TextureRect textureRect;

    float yOffset;

    public TexFloor(int mProgram, final float UNIT_SIZE, float yOffset, CollisionShape groundShape, DiscreteDynamicsWorld dynamicsWorld) {
        this.yOffset = yOffset;
        Transform groundTransform = new Transform();
        groundTransform.setIdentity();
        groundTransform.origin.set(new Vector3f(0.f, yOffset, 0.f));
        Vector3f localInertia = new Vector3f(0, 0, 0);

        DefaultMotionState myMotionState = new DefaultMotionState(groundTransform);

        RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(0, myMotionState, groundShape, localInertia);

        RigidBody body = new RigidBody(rbInfo);

        body.setRestitution(0.4f);

        body.setFriction(0.8f);

        dynamicsWorld.addRigidBody(body);

        textureRect = new TextureRect(UNIT_SIZE);

        initVertexData(UNIT_SIZE);
        intShader(mProgram);
    }

    public void initVertexData(final float UNIT_SIZE) {


        float[] vertices = new float[]
                {
                        1 * UNIT_SIZE, yOffset, 1 * UNIT_SIZE,
                        -1 * UNIT_SIZE, yOffset, -1 * UNIT_SIZE,
                        -1 * UNIT_SIZE, yOffset, 1 * UNIT_SIZE,

                        1 * UNIT_SIZE, yOffset, 1 * UNIT_SIZE,
                        1 * UNIT_SIZE, yOffset, -1 * UNIT_SIZE,
                        -1 * UNIT_SIZE, yOffset, -1 * UNIT_SIZE,
                };
        textureRect.setVertexBuffer(vertices);
        float[] textures = new float[]
                {
                        UNIT_SIZE / 2, UNIT_SIZE / 2, 0, 0, 0, UNIT_SIZE / 2,
                        UNIT_SIZE / 2, UNIT_SIZE / 2, UNIT_SIZE / 2, 0, 0, 0
                };
        textureRect.setTexCoorBuffer(textures);


    }

    public void intShader(int mProgram) {
        textureRect.initShader(mProgram);
    }

    public void drawSelf(int texId) {

        textureRect.drawSelf(texId);
    }
}
